package com.kr.redeye.project;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.kr.redeye.dto.ProjectMemberTableDto;
import com.kr.redeye.dto.ProjectTableDto;

@Service("projectService")
public class ProjectService {
	@Resource(name="projectDao")
	private ProjectDao projectDao;
	
	
	public int insert(ProjectTableDto param, HttpServletRequest request) {
		HttpSession session=request.getSession();
		param.setPROJECT_KEY("SVE"+Long.toString(System.currentTimeMillis()));
		param.setVE_MEMBER_IDX((int)session.getAttribute("VE_MEMBER_IDX"));
		if(param.getPROJECT_STEP().equals("SMART_VE")){
			param.setPROJECT_STEP("VE");
		}
		else{
			param.setPROJECT_STEP("PERF");
		}
		
		int checkNo=projectDao.insert(param);
		int PROJECT_IDX=projectDao.getProjectIDX(param);
		
//		System.out.println("random : "+Math.random());
		ProjectMemberTableDto dto= new ProjectMemberTableDto();
		
		if(checkNo>0){
			dto.setJOIN_STATE("JOIN");
			dto.setJOIN_TYPE("LEADER");
			dto.setLEADER_IDX((int)session.getAttribute("VE_MEMBER_IDX"));
			dto.setPROJECT_IDX(PROJECT_IDX);
			dto.setVE_MEMBER_IDX((int)session.getAttribute("VE_MEMBER_IDX"));
			if(param.getPROJECT_TYPE().equals("SMART_VE")){
				dto.setPROJECT_STEP("VE");
			}
			else{
				dto.setPROJECT_STEP("PERF");
			}
			int succ=projectDao.insertProjectMember(dto);
			return succ;
		}
		else{
			return -1;
		}
		
		
	}
	
	public List<HashMap<String, String>> getList(ProjectTableDto param, HttpServletRequest request) {
		HttpSession session=request.getSession();
		param.setVE_MEMBER_IDX((int)session.getAttribute("VE_MEMBER_IDX"));
		return projectDao.getList(param);
	}

	

}
